package com.example.service;


import com.example.entity.Book;
import com.example.entity.Room;
import com.example.exception.CustomerException;
import com.example.mapper.RoomMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomService {
    @Autowired
    private BookService bookService;
    @Autowired
    private RoomMapper roomMapper;
    public List<Room> selectAll(Room room){
        return roomMapper.selectAll(room);
    }

    public PageInfo<Room> selectPage(Integer pageNum, Integer pageSize,Room room) {
        PageHelper.startPage(pageNum,pageSize);
        List<Room> list = roomMapper.selectAll(room);
        return PageInfo.of(list);

    }

    public void add(Room room) {
        roomMapper.insert(room);
    }

    public void update(Room room) {
        roomMapper.updateById(room);
    }

    public void deleteById(Integer id) {
//        Book book = bookService.selectByRoomId(id);
//        if (book==null){
//            roomMapper.deleteById(id);
//        }else {
//            throw new CustomerException("该分类下存在书籍，无法删除");
//        }
        roomMapper.deleteById(id);
    }


    @Transactional
    public void deleteBatch(List<Integer> idList) {
        for (Integer id : idList) {
//            roomMapper.deleteById(id);
            this.deleteById(id);
        }
    }
}
